<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="domain.entity.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path;
	ServletContext context = pageContext.getServletContext();
	SysUser user = (SysUser) context.getAttribute("user");
%>
<script>
    $(function () {
        $('#ddlroles_userlist').combobox({
            multiple: true,
            url: '<%=basePath%>/system/role/getAllRoles',
            valueField: 'roleId',
            textField: 'name'
        });
        $('#txtdepartment_userlist').combotree({
            url: '<%=basePath%>/system/department/getDepartmentTreeData',
            width: '20%',
            editable:true,
            loadFilter: function (rows) {
                return convert(rows);
            }
        });
        InitUserList();
        var stor = $("#userlistgrid").edatagrid("getPager");
        if (stor) {
            $(stor).pagination({
                onSelectPage: function (pageNumber, pageSize) {
                    var para = $("#userlistgrid").datagrid("options");
                    para.queryParams = {
                        filter: $("#storefilter").val(),
                        //depart: $('#txtdepartment_userlist').combotree('getValue').,
                        //role: $('#ddlroles_userlist').combobox('getValues').join(',')
                    };
                    para.pageNumber = pageNumber;
                    para.pageSize = pageSize;
                    $('#userlistgrid').datagrid('reload');
                }
            });
        }
        showupdateWin();
    });


    function InitUserList() {
        $("#userlistgrid").edatagrid({
            title: '用户列表', //列表的标题
            iconCls: 'icon-site',
            width: 'auto',
            nowrap: true, //True 就会把数据显示在一行里。
            striped: true, //True 就把行条纹化。（即奇偶行使用不同背景色）
            collapsible: true, //可调节列宽
            url: "<%=basePath%>/system/user/getUserList", //获取数据的url
            columns: [
              [
                        { field: 'ck', checkbox: 'true' },
                          { field: 'userId', title: '用户ID', align: 'center', hidden: true },
                        { field: 'loginName', title: '账号', width: '10%', align: 'center' },
                        { field: 'realName', title: '真实姓名', width: '8%', align: 'center' },
                        { field: 'job', title: '职位', width: '8%', align: 'center' },
                         { field: 'department', title: '部门', width: '8%', align: 'center' },
                        { field: 'roles', title: '角色', width: '15%', align: 'center' },
                        {
                            field: 'enable', title: '用户状态', width: '8%', align: 'center', formatter: function (value, rec, index) {
                                if (rec.enable == true)
                                {
                                    return "<label>启用</label>"
                                }
                                else
                                {
                                    return "<label>禁用</label>"
                                }
                            }
                        },
                        {
                            field: 'enableLogin', title: '登录状态', width: '8%', align: 'center', formatter: function (value, rec, index)
                            {
                                if (rec.enableLogin == true)
                                {
                                    return "<label>启用</label>"
                                }
                                else
                                {
                                    return "<label>禁用</label>"
                                }
                            }
                        },
                        {
                            field: 'optorder', title: '操作', width: "35%", align: 'center', formatter: function (value, rec, index)
                            {
                                var a = '';
                                if (rec.enable == true)
                                {
                                    a += '<a href="javascript:enableUser(\'' + rec.userId + '\',\'' + "0" + '\')" mce_href="#">禁用</a> ';
                                }
                                else
                                {
                                    a += '<a href="javascript:enableUser(\'' + rec.userId + '\',\'' + "1" + '\')" mce_href="#">启用</a> ';
                                }
                             
                                if (rec.enableLogin == true) {
                                    a += '<a href="javascript:enableLoginUser(\'' + rec.userId + '\',\'' + "0" + '\')" mce_href="#">禁用登录</a> ';
                                }
                                else {
                                    a += '<a href="javascript:enableLoginUser(\'' + rec.userId + '\',\'' + "1" + '\')" mce_href="#">启用登录</a> ';
                                }

                                a += '<a href="javascript:updateusername(\'' + rec.userId + '\',\'' + rec.realName + '\',\'' + rec.job + '\')" mce_href="#">编辑</a> ';
                                a += '<a href="javascript:initpwd(\'' + rec.userId + '\')" mce_href="#">初始化密码</a> ';
                                a += '<a href="javascript:edituserauthrization(\'' + rec.userId + '\')" mce_href="#">权限</a> ';
                                return a;
                            }
                        }
              ]
            ],
            pagination: true,
            rownumbers: true,
            pageSize: '20'
        });
    }

    function userquery() {
        $("#userlistgrid").datagrid("load", {
            filter: $("#userfilter").val(),
            //depart: $('#txtdepartment_userlist').combotree('getValue'),
           // role: $('#ddlroles_userlist').combobox('getValues').join(',')
        });
    }

    function enableUser(id,flag) {
        $.post('<%=basePath%>/system/user/enableData?id=' + id + '&flag=' + flag, null, function (data) {
                ERP_alert(data);
                userquery();
        });
    } 
    function enableLoginUser(id,flag) {
        $.post('<%=basePath%>/system/user/enableLoginData?id=' + id + '&flag=' + flag, null, function (data) {
        	 ERP_alert(data);
             userquery();
        });
    }

    function updateusername(userId,username,job) {
        $("#txtusername").val(username);
        $("#txtjob_userlist").val(job);
        $('#updateWin').dialog("open");
    }

    function initpwd(userId) {
        $.messager.confirm('提示信息', '确定初始化密码吗？', function (r) {
            if (r) {
                var postdata = {
                    userId: userId
                };
                $.post("<%=basePath%>/system/user/initPwdData", postdata, function (data) {
                	if(data=="")
                		  ERP_alert('初始化操作成功');
                	else
                    ERP_alert(data);
                });
            }
        });
    }

    function showupdateWin() {
        $('#updateWin').dialog(
            {
                title: '用户名',
                height: 150,
                width: 350,
                closed: true,
                cache: false,
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: function () {
                        var checkID = $('#userlistgrid').datagrid('getSelected').userId;
                        var realName = $("#txtusername").val();
                        var job = $("#txtjob_userlist").val();
                        if (realName == "")
                        {
                            ERP_alert("用户名不能为空！！");
                            return false;
                        }
                        var row = $('#userlistgrid').datagrid('getSelected');
                        var rowIndex = $('#userlistgrid').datagrid('getRowIndex', row);
                        var postdata = {
                            username: realName,
                            job:job
                        };
                        $.post("<%=basePath%>/system/user/updateUser?id=" + checkID, postdata, function (data) {
                            if (data== "") {
                                userquery();
                                $('#updateWin').dialog("close");
                                return false;
                            }
                            ERP_alert(data);
                        });
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        $('#updateWin').dialog("close");
                    }
                }]
            }
        );
    }
    function edituserauthrization(id) {
        $('#storedetail').dialog({
            title:'角色',
            width: 400,
            height: 300,
            closed: false,
            cache: false,
            href: '<%=basePath%>/system/user/editUserAuthrization?id=' + id,
			modal : true
		});

	}

	function movedepartment() {
		var rows = $('#userlistgrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('操作提示', '请选择数据!', 'warning');
			return false;
		}
		var itemIdList = "";
		for (var i = 0; i < rows.length; i++) {
			itemIdList += rows[i]["userId"] + ",";
		}
		$.post('<%=basePath%>/system/user/moveToDepartment', {
			users : itemIdList,
			partid : $('#txtdepartment_userlist').combotree('getValue')
		}, function(data) {
			if (data == "") {
				ERP_alertInfo('操作成功');
				userquery();
			} else {
				ERP_alertError(data);
			}
		})
	}
	function moveroles() {
		var rows = $('#userlistgrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('操作提示', '请选择数据!', 'warning');
			return false;
		}
		var itemIdList = "";
		for (var i = 0; i < rows.length; i++) {
			itemIdList += rows[i]["userId"] + ",";
		}
		$.post('<%=basePath%>/system/user/moveToRoles', {
			users : itemIdList,
			roles : $('#ddlroles_userlist').combobox('getValues').join(',')
		}, function(data) {
			if (data == "") {
				ERP_alertInfo('操作成功');
				userquery();
			} else {
				ERP_alertError(data);
			}
		})
	}
</script>

<div id="toolbar_filter">
	关键字： <input id="userfilter" type="text" class="easyui-textbox"
		data-options="prompt:'账号/真实姓名'" />&nbsp;&nbsp; <a
		href="javascript:userquery()" class="easyui-linkbutton"
		data-options="iconCls:'icon-search'">查询</a>&nbsp;&nbsp;&nbsp;&nbsp; <a
		href="javascript:movedepartment()" class="easyui-linkbutton">移动到</a>部门：<input
		id="txtdepartment_userlist">&nbsp;&nbsp; <a
		href="javascript:moveroles()" class="easyui-linkbutton">授权到</a>角色：<input
		id="ddlroles_userlist">&nbsp;&nbsp;
</div>

<table id="userlistgrid"></table>
<div id="storedetail"></div>
<div id="storeEditdetail"></div>

<div id="updateWin">
	<table class="form-table" style="text-align: center">
		<tr>
			<td class="form-label">用&nbsp;户&nbsp;名<span style="color: red">*</span>:
			</td>
			<td><input class="easyui-textbox" name="txtusername"
				id="txtusername" type="text"></td>
		</tr>
		<tr>
			<td class="form-label">职&nbsp;&nbsp;位<span style="color: red">*</span>:
			</td>
			<td><input class="easyui-textbox" name="txtjob_userlist"
				id="txtjob_userlist" type="text"></td>
		</tr>
	</table>
</div>